package com.web.domain;

/**
 * Created by Administrator on 2017/3/6.
 */

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.List;
import java.util.Map;

/**
 * api 返回类
 *
 * @author ekoo
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Response<T> implements Serializable {
    public static Integer SUCCESS=900000;
    public static Integer FAIL=999999;
    private Integer status;/**900000成功，非900000失败**  900000-999999*/
    private String message;//错误具体内容
    protected  T params;
    private long timestamp;
    public Response failure() {
        this.status=FAIL;
        this.message="fail";
        return this;
    }
    public Response failure(Integer status, String message) {
        this.status=status;
        this.message=message;
        return this;
    }
    public Response failure(String message) {
        this.status=FAIL;
        this.message=message;
        return this;
    }

    public Response ok() {
        this.status=SUCCESS;
        this.message="成功！";
        return this;
    }
    public Response ok(String message) {
        this.status=SUCCESS;
        this.message=message;
        return this;
    }
    @JsonIgnore
    public Boolean isOk(){
        if(status==SUCCESS)
            return true;
        else return false;
    }
    public static Response custom(){
        Response response=new Response();
        return response;
    }
    public static Response List(){
        Response<List> response=new Response<List>();
        if(response.getParams()==null){
            response.setParams(Lists.newArrayList());
        }
        return response;
    }
    public static Response Map(){
        Response<Map> response=new Response<Map>();
        if(response.getParams()==null){
            response.setParams(Maps.newHashMap());
        }
        return response;
    }
    public static Response Map(String key, Object value){
        Response<Map> response=new Response<Map>();
        if(response.getParams()==null){
            response.setParams(Maps.newHashMap());
            response.getParams().put(key,value);
        }
        return response;
    }
    public static Response Map(Map map){
        Response<Map> response=new Response<Map>();
        if(response.getParams()==null){
            response.setParams(Maps.newHashMap());
            response.getParams().putAll(map);
        }
        return response;
    }
    public Response addParams(T params) {
        this.params = params;
        return this;
    }


    public String getMessage() {
        return StringUtils.defaultString(message);
    }

    public long getTimestamp() {
        if(timestamp==0){
            return System.currentTimeMillis();
        }else
            return timestamp;
    }

    public Integer getStatus() {
        return status;
    }

    public void setStatus(Integer status) {
        this.status = status;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getParams() {
        return params;
    }

    public void setParams(T params) {
        this.params = params;
    }

    public void setTimestamp(Long timestamp) {
        this.timestamp = timestamp;
    }
}